<?php
/**
 * ブログ記事一つを表すモデル
 *
 * @group model
 */
class Category extends DataModel
{
    const MODEL_ID    = 'category_type';
    
    protected $_schema = array(
        'category_id'   => parent::INTEGER,
        'slug'          => parent::TEXT,
        'name'          => parent::TEXT,
        'note'          => parent::TEXT,
        'editor_note'   => parent::TEXT,
        'sort'          => parent::INTEGER,
        'mode'          => parent::INTEGER,
        'update_date'   => parent::DATETIME,
        'publish_date'  => parent::DATETIME,
        
        'category_list_id'  => parent::INTEGER,
        'param_count'       => parent::INTEGER,
        
        'relation'      => parent::TEXT,
        'parameter_1'   => parent::TEXT,
        'parameter_2'   => parent::TEXT,
        'parameter_3'   => parent::TEXT,
        'parameter_4'   => parent::TEXT,
        'parameter_5'   => parent::TEXT,
        'parameter_6'   => parent::TEXT,
        
        'total'         => parent::INTEGER,
    /*
        'id'            => parent::INTEGER,
        'param'         => parent::TEXT,
        
        'child'         => parent::BOOLEAN
     * */
    );

    function isValid(Array $error = null)
    {
        if (isset($this->_edit_hito)) {
            return null;
        }
        else if (isset($this->_edit_profile)) {
            return null;
        }
        else if (isset($this->_edit_child)) {
        }
        
        
        $val = $this->name;
        if (static::checkLength($val, 32, 1)) {
            $error['name'] = '1文字以上32文字以下';
        }
        
        $val = $this->note;
        if (static::checkLength($val, 1024, 0)) {
            $error['note'] = '1024文字以内';
        }
        
        $val = $this->editor_note;
        if (static::checkLength($val, 1024, 0)) {
            $error['editor_note'] = '1024文字以内';
        }

        $val = $this->category_id;
        if (static::checkId($val)) {
            $error['category_id'] = '不正な文字列（半角英数字のみ）';
        }
        
        if (!isset($this->child)) {

            $val = $this->slug;
            if (static::checkLength($val, 32, 1) || static::checkLowerAlphabet($val)) {
                $error['slug'] = '1文字以上16文字以下の半角英小文字';
            }
            
        } else {
            $val = $this->category_id;
            if (static::checkId($val)) {
                $error['category_id'] = '不正な文字列（半角英数字のみ）';
            }
            
            /*
            $val = $this->multiple;
            if ($val) $this->multiple = 1;
            else $this->multiple = 0;
            */
        }

        return $error;
    }
    
}
